package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.ParkStandardSpecificationMapper;
import com.bf.electroplating.pojo.dto.standardspecification.ParkStandardSpecificationQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComDischargePermit;
import com.bf.electroplating.pojo.entity.ParkStandardSpecification;
import com.bf.electroplating.pojo.enums.AttModuleEnums;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.pollution.PollutionPermitVO;
import com.bf.electroplating.pojo.vo.standardspecification.ParkStandardSpecificationVO;
import com.bf.electroplating.service.ParkStandardSpecificationService;
import com.bf.electroplating.service.PubAttachmentService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ParkStandardSpecificationServiceImpl extends ServiceImpl < ParkStandardSpecificationMapper,
		ParkStandardSpecification > implements ParkStandardSpecificationService {

	@Resource
	private PubAttachmentService pubAttachmentService;

	@Override
	public ResultVO < Page < ParkStandardSpecificationVO > > pageList ( ParkStandardSpecificationQueryDTO queryDTO ) {
		Page < ParkStandardSpecification > page = new Page <> ( queryDTO.getPageNum () , queryDTO.getPageSize () );
		LambdaQueryWrapper < ParkStandardSpecification > wrapper = new LambdaQueryWrapper <> ();

		if ( StringUtils.isNotBlank ( queryDTO.getDescription () ) ) {
			wrapper.like ( ParkStandardSpecification::getDescription , queryDTO.getDescription () );
		}
		if ( StringUtils.isNotBlank ( queryDTO.getFileName () ) ) {
			wrapper.like ( ParkStandardSpecification::getFileName , queryDTO.getFileName () );
		}

		wrapper.orderByDesc ( ParkStandardSpecification::getCreateTime );

		Page < ParkStandardSpecification > resultPage = this.page ( page , wrapper );

		// 转换为VO
		Page < ParkStandardSpecificationVO > voPage = new Page <> ();
		BeanUtils.copyProperties ( resultPage , voPage , "records" );

		List < ParkStandardSpecificationVO > voList = new ArrayList <> ();
		for ( ParkStandardSpecification entity : resultPage.getRecords () ) {
			ParkStandardSpecificationVO vo = new ParkStandardSpecificationVO ();
			BeanUtils.copyProperties ( entity , vo );
			vo.setAttachmentVoList ( pubAttachmentService.getWholeFile ( entity.getId () ,
					AttModuleEnums.STANDARD_SPECIFICATION.getCode () ) );
			voList.add ( vo );
		}
		voPage.setRecords ( voList );

		return ResultVO.ok ( voPage );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > saveData ( ParkStandardSpecificationVO dto ) {
		ParkStandardSpecification standardSpecification = new ParkStandardSpecification ();
		BeanUtils.copyProperties ( dto , standardSpecification );
		saveOrUpdate ( standardSpecification );
		pubAttachmentService.saveWholeAttachmentRel ( dto.getAttachmentVoList () , standardSpecification.getId () , AttModuleEnums.STANDARD_SPECIFICATION.getCode () );
		return ResultVO.ok ( null , "保存成功" );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > delete ( List < String > ids ) {
		if ( ids == null || ids.isEmpty () ) {
			return ResultVO.fail ( "ID不能为空" );
		}
		for ( String id : ids ) {
			pubAttachmentService.deleteByModuleId (  id , AttModuleEnums.STANDARD_SPECIFICATION.getCode () );
		}
		this.removeByIds ( ids );
		return ResultVO.ok ( null , "删除成功" );
	}

	@Override
	public ResultVO < ParkStandardSpecificationVO > getDetail ( String id ) {
		ParkStandardSpecification entity = this.getById ( id );
		ParkStandardSpecificationVO vo = new ParkStandardSpecificationVO ();
		BeanUtils.copyProperties ( entity , vo );
		vo.setAttachmentVoList ( pubAttachmentService.getWholeFile ( entity.getId () ,
				AttModuleEnums.STANDARD_SPECIFICATION.getCode () ) );
		return ResultVO.ok ( vo );
	}
}
